package com.raowb.tool.seal.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import com.raowb.tool.seal.util.seal.SealCircle;
import com.raowb.tool.seal.util.seal.SealFont;
import com.raowb.tool.seal.util.seal.SealMain;
import com.raowb.tool.seal.util.seal.SealUtil;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.raowb.common.base.controllers.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.raowb.tool.seal.domain.ToolSeal;
import com.raowb.tool.seal.service.IToolSealService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 印章Controller
 * 
 * @author raowb
 * @date 2022-03-22
 */
@RestController
@RequestMapping("/tool/seal")
public class ToolSealController extends BaseController
{
    @Autowired
    private IToolSealService toolSealService;

    /**
     * 查询印章列表
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:list')")
    @GetMapping("/list")
    public TableDataInfo list(ToolSeal toolSeal)
    {
        return getDataTable(toolSealService.findPage(getPage(),getQueryWrapper(toolSeal)));
    }

    /**
     * 导出印章列表
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:export')")
    @Log(title = "印章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ToolSeal toolSeal)
    {
        List<ToolSeal> list = toolSealService.findList(getQueryWrapper(toolSeal));
        ExcelUtil<ToolSeal> util = new ExcelUtil<ToolSeal>(ToolSeal.class);
        util.exportExcel(response, list, "印章数据");
    }

    /**
     * 获取印章详细信息
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(toolSealService.findById(id));
    }

    /**
     * 新增印章
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:add')")
    @Log(title = "印章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ToolSeal toolSeal)
    {
        return toAjax(toolSealService.save(toolSeal));
    }

    /**
     * 修改印章
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:edit')")
    @Log(title = "印章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ToolSeal toolSeal)
    {
        return toAjax(toolSealService.update(toolSeal));
    }

    /**
     * 删除印章
     */
    @PreAuthorize("@ss.hasPermi('tool:seal:remove')")
    @Log(title = "印章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(toolSealService.delete(ids));
    }


    /**
     * 画印章
     */
    @PostMapping("/drawSeal")
    public AjaxResult drawSeal(@RequestBody SealMain sealMain)
    {
        //设置画笔颜色
        sealMain.setBackgroudColor(Color.RED);

        AjaxResult ajax = AjaxResult.success();

        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try
        {
            BufferedImage image = SealUtil.buildSeal(sealMain);
            ImageIO.write(image, "png", os);
            //SealUtil.storeBytes(os.toByteArray(),"C:\\temp\\公章2.png");
        }
        catch (IOException e)
        {
            return AjaxResult.error(e.getMessage());
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        ajax.put("img", Base64.encode(os.toByteArray()));
        return ajax;
    }

    /**
     * 获取系统字体
     * @return
     */
    @GetMapping(value = "/getFontFamily")
    public AjaxResult getFontFamily()
    {
        String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        return AjaxResult.success(fontNames);
    }

}
